package com.answer.controller; 

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.answer.util.SignUtil;


@Controller
@RequestMapping("/weixin")
public class WeinxinController {

	/**
	 * 
	 * 微信接入接口
	 * @author songbaoming
	 * @date 2017年4月27日 
	 * @param signature  微信加密签名
	 * @param timestamp	  时间戳
	 * @param nonce		随机数
	 * @param echostr	随机字符串
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = { "signature" }, method = RequestMethod.GET)
	public void signature(@RequestParam(value = "signature", required = true) String signature,
			@RequestParam(value = "timestamp", required = true) String timestamp,
			@RequestParam(value = "nonce", required = true) String nonce,
			@RequestParam(value = "echostr", required = true) String echostr, HttpServletResponse response)
			throws IOException {
		PrintWriter out = response.getWriter();
		// 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
		if (SignUtil.checkSignature(signature, timestamp, nonce)) {
			out.print(echostr);
			System.out.println("微信服务验证成功！");
		}
		out.close();
		out = null;
	}
}
